//
//  AppVersionTipsView.m
//  CedarFinancePH
//
//  Created by lshenrong on 2018/8/20.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "AppVersionTipsView.h"
#include <UIView+TYAlertView.h>

@interface AppVersionTipsView()<UITextViewDelegate>

@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *messae;
@property(nonatomic,assign) BOOL isForce;//是否强制

@end

@implementation AppVersionTipsView

- (instancetype)initWithForceTitle:(NSString *)title message:(NSString *)messae{
    return [self initWithForceTitle:title message:messae force:YES];
}

- (instancetype)initWithForceTitle:(NSString *)title message:(NSString *)messae force:(BOOL)isForce{
    if (self = [super init]) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 12.0;
        self.layer.masksToBounds = YES;
        self.isForce = isForce;
        self.title = title;
        if([messae containsString:@"@@@"]){
            self.messae =  [messae stringByReplacingOccurrencesOfString:@"@@@" withString:@"\n"];
        }else{
            self.messae =  messae;
        }
        CGFloat height = 0;
        CGFloat maxSize = SCREEN_WIDTH -  60 - 30;
        UIFont *font = PingFangZhongChangGuiForFont(18);
        CGSize size = [PlatformTools sizeWithString:self.messae andFount:font andMaxSize:CGSizeMake(maxSize, MAXFLOAT)];
        CGFloat textViewHeight = 0;
        
        CGFloat maxHeight = SCREEN_HEIGHT - 100;
        CGFloat fixedHeight = 310;
        CGFloat offsexHeight = maxHeight  - fixedHeight;
        if (size.height>offsexHeight) {
            size.height = offsexHeight;
            textViewHeight = offsexHeight;
        }
        height = size.height + fixedHeight;
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH - 60, height);
        [self setupSubviews:textViewHeight];
    }
    return self;
}

- (void)setupSubviews:(CGFloat)setTextViewMaxHeight{
/*
    CedarButton *closeBtn = [CedarButton buttonWithImageName:@"Close"];
    [self addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).mas_offset(-12);
        make.top.equalTo(self).mas_offset(10);
    }];*/
    
    UIImage *image = [UIImage imageNamed:@"AppDownload"];
    UIImageView *imageView = [[UIImageView alloc]initWithImage:image];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [self addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.equalTo(self).offset(15);
        make.size.mas_offset((CGSizeMake(166, 164)));
    }];
    
    
    UILabel *titleLbl = [[UILabel alloc]init];
    NSString *title = @"发现新版本";//   [NSString stringWithFormat:@"发现新版本: %@",self.title];
    titleLbl.textColor = kBlackColor;
    titleLbl.text = title;
    titleLbl.font = PingFangZhongChangGui_MediumForFont(18);
    titleLbl.numberOfLines = 0;
    [self addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(imageView.mas_bottom).mas_offset(20);
    }];
    
    UITextView *textView =  [[UITextView alloc]init];
    textView.scrollEnabled = setTextViewMaxHeight>0?YES:NO;
    textView.text =  self.messae;
    textView.delegate = self;
    textView.font = PingFangZhongChangGuiForFont(16);
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(15);
        make.right.mas_equalTo(self).offset(-15);
        make.top.mas_equalTo(titleLbl.mas_bottom).offset(13);
        if(setTextViewMaxHeight>0){
            make.height.mas_offset(setTextViewMaxHeight);
        }
    }];

    CedarButton *updateBtn =  [CedarButton buttonWithRegisterText:@"更新"];
    @weakify(self)
    updateBtn.clickEvent = ^{
        @strongify(self)
        [self hideView];
        if (self.updateHandler) {
            self.updateHandler();
        }
    };
    [updateBtn PFCGStyleFontSize:18.0];
    [self addSubview:updateBtn];
    [updateBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.bottom.mas_offset(-15);
        make.height.mas_offset(36);
        make.width.mas_offset(240);
    }];
    
    if(!self.isForce){
        CedarButton *closeBtn = [CedarButton buttonWithImageName:@"Close"];
        @weakify(self)
        closeBtn.clickEvent = ^{
            @strongify(self)
            [self hideView];
        };
        [self addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).mas_offset(-12);
            make.top.equalTo(self).mas_offset(10);
        }];
    }
//    UIView *hLineView =  [[UIView alloc]init];
//    hLineView.backgroundColor = kSeparateLineColor;
//    [self addSubview:hLineView];
//    [hLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(self);
//        make.bottom.mas_offset(-46);
//        make.height.mas_offset(0.5);
//    }];
//
}

-(BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}
@end
